<template>
	<el-dialog v-model="visible" :title="type == 'edit' ? '编辑字段' : '新增字段'" :width="600" :close-on-click-modal="false">
		<el-form ref="dataFormRef" label-width="100" :model="dataForm" :rules="dataRules">
			<el-form-item prop="fieldCode" label="字段名称" :disabled="type == 'edit'">
				<el-select v-model="dataForm.fieldCode" :disabled="type == 'edit'" placeholder="请选择字段名称" style="width: 100%">
					<el-option label="患者姓名" value="ptt_nm"></el-option>
					<el-option label="性别" value="gdr_cd"></el-option>
					<el-option label="身份证号" value="id_crd_no"></el-option>
					<el-option label="手机号" value="slf_tel_no"></el-option>
					<el-option label="出生日期" value="bth_dt"></el-option>
					<el-option label="现住址" value="address"></el-option>
				</el-select>
			</el-form-item>
			<el-form-item prop="algoCode" label="选择算法" :disabled="type == 'edit'">
				<el-select v-model="dataForm.algoCode" :disabled="type == 'edit'" placeholder="请选择算法" style="width: 100%">
					<el-option label="余弦相似度" value="1"></el-option>
					<el-option label="精准匹配" value="2"></el-option>
				</el-select>
			</el-form-item>
			<el-form-item prop="status" label="状态">
				<el-switch v-model="dataForm.status"></el-switch>
			</el-form-item>
			<el-form-item prop="weight" label="匹配权重">
				<el-input-number style="width: 94%" :max="100" :precision="0" :step="1" :min="0" v-model="dataForm.weight" placeholder="请输入匹配权重" />
				&nbsp; %
			</el-form-item>
			<el-form-item prop="remark" label="备注">
				<el-input v-model="dataForm.remark" :rows="4" maxlength="200" type="textarea" show-word-limit></el-input>
			</el-form-item>
		</el-form>

		<template #footer>
			<el-button @click="visible = false">取消</el-button>
			<el-button type="primary" @click="saveFormBtn">确定</el-button>
		</template>
	</el-dialog>
</template>
<script setup lang="ts">
import { ref, reactive, nextTick } from 'vue'
import { ElMessage } from 'element-plus'
import timeFormater from 'time-formater'

const visible = ref(false)
const dataFormRef = ref()
let dataForm = ref({
	fieldCode: '',
	algoCode: '',
	status: true,
	remark: '',
	ifInvalid: 0,
	createTime: '',
	weight: 0
})

const emit = defineEmits(['close'])

const dataRules = ref({
	fieldCode: [
		{
			required: true,
			trigger: 'change',
			validator: (rule, value, callback) => {
				if (type.value == 'edit') {
					callback()
				} else {
					if (!value) {
						callback(new Error('字段名称不能为空'))
					} else {
						console.log(listData.value)
						listData.value.filter(item => item.fieldCode == value).length > 0 ? callback(new Error('该字段名称已存在，请勿重复添加')) : callback()
					}
				}
			}
		}
	],
	algoCode: [{ required: true, message: '选择算法不能为空', trigger: 'change' }],
	// remark: [
	// 	{
	// 		validator: (rule, value, callback) => {
	// 			if (value && value.length) {
	// 				if (value.length > 200) {
	// 					dataForm.value.remark = value.slice(0, 200)
	// 					callback(new Error('备注不能超过200个字符'))
	// 				} else {
	// 					callback()
	// 				}
	// 			} else {
	// 				callback()
	// 			}
	// 		}
	// 	}
	// ]
})

const type = ref()
const dataIndex = ref()
const listData = ref([])

const init = (id, btntype, data, fieldruleId, listDatas) => {
	dataIndex.value = id
	type.value = btntype
	visible.value = true
	listData.value = listDatas

	nextTick(() => {
		if (dataFormRef.value) {
			dataFormRef.value.resetFields()
		}

		if (btntype == 'edit') {
			// Object.assign(dataForm.value, data)
			dataForm.value = JSON.parse(JSON.stringify(data))

			dataForm.value.status = dataForm.value.ifInvalid == 0 ? true : false
		}

		if (btntype == 'add') {
			// dataForm.value = {
			// 	fieldCode: '',
			// 	algoCode: '',
			// 	status: true,
			// 	remark: '',
			// 	ifInvalid: 0,
			// 	createTime: '',
			// 	weight: 0
			// }
			dataForm.value.ruleId = fieldruleId
			delete dataForm.value.id
		}
	})
}

const saveFormBtn = () => {
	dataFormRef.value.validate(valid => {
		if (valid) {
			// 表单验证通过，执行保存操作

			if (dataForm.value.status) {
				dataForm.value.ifInvalid = 0
			} else {
				dataForm.value.ifInvalid = 1
			}

			if (!dataForm.value.weight) {
				dataForm.value.weight = 0
			}

			if (type.value == 'edit') {
			} else {
				delete dataForm.value.status
				dataForm.value.createTime = timeFormater(new Date()).format('YYYY-MM-DD HH:mm:ss')
				dataForm.value.tm = new Date().getTime() + new Date().getMilliseconds()
			}

			visible.value = false
			emit('close', dataForm.value, type.value, dataIndex.value)
		}
	})
}

defineExpose({
	init
})
</script>
<style lang="scss" scoped></style>
